$(function(){
    // window.location.reload() 
	const userMoney = $('#userMoney'),
		RMB = $('#RMB'),
		tkbox = $('#tkbox'),
		messge = $('#messge'),
		noBtn = $('#no'),
		yesBtn = $('#yes'),
		submit = $('#submit');


	function GetQueryString (name) { //获取指定name的url参数
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return (r[2]);
        return null;
    }
    function Toast (txt){
        var toastHtml = '<div class="tk_info" id="tkInfo"></div>';
        var toast = $('body #tkInfo');
        if (toast.length) {
            toast.html(txt).show();
        }else{
            $('body').append(toastHtml);
            $('body #tkInfo').html(txt).show();
        }
        setTimeout(function(){
          $('body #tkInfo').hide();
        },3000)
    }

    const userId = GetQueryString('userId');
    const token = GetQueryString('token');
    let codeYQM = '';
    let host = 'http://'+GetQueryString('invokeDomain');
    // let watchTime = GetQueryString('videoTime');
    //获取用户信息
    let bindZfbState = false;
    function getUserInfo(){
    	$.ajax({
    		type:'get',
            dataType: 'json',
            url:host+'/system/api/appuser/balance',
            data: {
            	'userId':userId,
            	'token':token
            },
            //请求成功时执行
            success: function(v) {
                const code = v.code;
                const msg = v.msg;
                const data = v.data.info;
                if (code == 0) {
                	let money = data.money;
                    codeYQM = v.data.code;
                   	RMB.html(Math.floor(money/100)/100)
                   	userMoney.html(money)
                    $('.right').attr('href','./withdraw-list.html?userId='+userId+'&token='+token+'&invokeDomain='+GetQueryString('invokeDomain'))
                    if (data.zfb) {
                        bindZfbState = true;
                        $('.flex_info').html('<p>'+data.zfb+'</p><a href='+host+'/Vtask/bindZfb.html?userId='+userId+'&token='+token+'&invokeDomain='+GetQueryString("invokeDomain")+'>更换绑定</a>')
                    }else{
                        $('.flex_info').html('<a href='+host+'/Vtask/bindZfb.html?userId='+userId+'&token='+token+'&invokeDomain='+GetQueryString("invokeDomain")+'>绑定支付宝</a>')
                    }
                } else {
                    // 异常处理
                    console.log('去登录');
                    Toast(msg||'网络错误')
                    // window.webkit.messageHandlers.MJLogin.postMessage(null)
                }
            },
            //请求失败遇到异常触发
            error: function(d) {
                console.log(d.msg);
            }
    	})
    }

    getUserInfo()
    let num = '1';

    //选择提现金额
    $('.flex li').on('click',function(){
    	if ($(this).hasClass('hover')) {
    		$(this).removeClass('hover')
    		num ='';
    	}else{
	    	num = $(this).attr('data-num');
	    	$('.flex li').removeClass('hover');
	    	$(this).addClass('hover')
    	}
    })

    //取消tkbox
    noBtn.on('click',function(){
    	tkbox.hide();
    	yesBtn.off("click");
    })
    tkbox.on('click',function(e){
    	var obj = $(e.target);
    	if (obj.attr('class') == 'tkbox') {
    		$(this).hide();
    		yesBtn.off("click");
    		return false;
    	}
    })
    // //提现
    submit.on('click',function(){
        const myMoney = $('#RMB').html()
    	if (!num) {
    		Toast('请选择提现金额！');
    		return;
    	}
        if (myMoney < num) {
            tkbox.show();
            messge.html('余额不足，去邀请好友观看！')
            yesBtn.on('click',function(){
                tkbox.hide();
                location.href = 'invite.html?userId='+userId+'&token='+token+'&code='+codeYQM+'&invokeDomain='+GetQueryString('invokeDomain');
            })
            return;
        }
        if (!bindZfbState) {
            tkbox.show();
            messge.html('未绑定支付宝，去绑定！')
            yesBtn.on('click',function(){
                tkbox.hide();
                location.href = host+'/Vtask/bindZfb.html?userId='+userId+'&token='+token+'&invokeDomain='+GetQueryString("invokeDomain");
            })
            return;
        }
    	$.ajax({
    		type:'get',
            dataType: 'json',
            url:host + '/system/api/transfer/zfbtransferMoney',
            data: {
            	'userId':userId,
            	'token':token,
            	'money':num,
            	'origin':'meijnios'
            },
            //请求成功时执行
            success: function(v) {
                const code = v.code;
                const msg = v.msg;
                const id = v.data;
                if (code == 0) {
                	location.href = 'withdraw-succe.html?id='+id+'&money='+num+'&userId='+userId+'&token='+token+'&invokeDomain='+GetQueryString('invokeDomain')+'&codeYQM='+codeYQM+'type=zfb';
                } else if(code == 602) {//观看不足
                    // 异常处理
                    tkbox.show();
                    messge.html('观看不足3分钟，去首页观看')
                    yesBtn.on('click',function(){
                    	console.log('去首页');
                        tkbox.hide();
                        H5brigeAPP.MJGoHome()
                    	// window.webkit.messageHandlers.MJGoHome.postMessage(null)
                    })
                }else if(code == 604) {//未关注公众号
                    // 异常处理
                    tkbox.show();
                    messge.html('<span class="p_info">未关注公众号，请去关注公众号“中拓时代”</span><a href="javascript:;" class="copy" data-clipboard-text="中拓时代" id="copyCode">复制公众号</a>');
                    var clipboard1 = new ClipboardJS('#copyCode');
                        clipboard1.on('success', function(e){
                            Toast('复制成功！')
                        });
                        clipboard1.on('error', function(e){
                            Toast('复制失败！')
                        });
                    yesBtn.html('去关注')
                    yesBtn.on('click',function(){
                    	console.log('去关注公众号');
                        tkbox.hide();
                        H5brigeAPP.MJWXOpen()
                    	// window.webkit.messageHandlers.MJWXOpen.postMessage(null)
                    })

                }else if(code == 605||code == 600) {//余额不足 600余额小于1
                    // 异常处理
                    tkbox.show();
                    messge.html('余额不足，去邀请好友观看！')
                    yesBtn.on('click',function(){
                        tkbox.hide();
                    	location.href = 'invite.html?userId='+userId+'&token='+token+'&code='+codeYQM+'&invokeDomain='+GetQueryString('invokeDomain');
                    })
                }else{
                	Toast(msg);
                }
            },
            //请求失败遇到异常触发
            error: function(d) {
                Toast(d.msg);
            }
    	})

    })
    //返回app
    $('.back').on('click',function(){
        // window.history.back();
        H5brigeAPP.NativeBack()
    })
})